﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 操作类
    /// 
    /// 修改纪录
    ///     2012-07-31 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class CityMobileHelper {
        /// <summary>
        /// 缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="cityMobile">实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(CityMobile cityMobile, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(CityMobile._)
                .Value(CityMobile._Mobile, cityMobile.Mobile)
                .Value(CityMobile._CityCode, cityMobile.CityCode)
                .Value(CityMobile._CityName, cityMobile.CityName)
                .Value(CityMobile._ProvinceCode, cityMobile.ProvinceCode)
                .Value(CityMobile._Province, cityMobile.Province)
                .Value(CityMobile._CstCode, cityMobile.CstCode)
                .Value(CityMobile._PostCode, cityMobile.PostCode)
                .ToString();
            bool istrue = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) == 1 : Data.Pool(dbkey).ExecSql(strSql) == 1;
            if (delCache.IsNull()) return istrue;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.CityMobileCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.CityMobileCache_" + s + ")" : s);
            }
            return istrue;
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="cityMobile">实体类</param>
        /// <returns>添加是否成功</returns>
        public static bool Insert(CityMobile cityMobile, string dbkey = "") {
            return Insert(cityMobile, dbkey, null);
        }
        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="mobile">手机号前7位</param>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>记录是否存在</returns>
        public static bool IsExistByID(string mobile,  string dbkey = "", string where = "") {
            string strSql = new SQL().Count(CityMobile._Mobile).From(CityMobile._)
                .Where("1=1").Where(new Where()
                    .And(CityMobile._Mobile, mobile, Operator.Equal)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.GetScalar(strSql).ToInt(0) :  Data.Pool(dbkey).GetScalar(strSql).ToInt(0);
            return value == 1 ? true : false;
        }
        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="mobile">手机号前7位</param>
        /// <returns>记录是否存在</returns>
        public static bool IsExistByID(string mobile, string dbkey = "") {
            return IsExistByID(mobile,  dbkey, "");
        }
    }
}

